package data.deal;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import dataconnect.ConnectionFactory;
import dataconnect.SqlUtil;

public class InfoLevel {
	public void deal(String code){
		Document doc = null;
		try {
			doc = Jsoup.connect("http://finance.sina.com.cn/realstock/company/"+code+"/nc.shtml").get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(doc==null){
			return;
		}
		
		Elements els = doc.getElementsByTag("script");
		
		int info=0;
		
		for (Element element : els) {
			String str = element.data();
			if(str.indexOf("var gradeLevel")>=0){
				String[] varStr = str.split("\n");
				for (int i = 0; i < varStr.length; i++) {
					if(varStr[i].indexOf("var gradeLevel")>=0){
						info = Integer.parseInt(varStr[i].substring(17, varStr[i].indexOf(";")));
						break;
					}
				}
			}
		}

		SqlUtil.update("update stockanlyz set info="+info+" where number ='"+code+"'");
		
	}
	
	public static void main(String[] args) {
		InfoLevel il = new InfoLevel();
		String sql = "select number from stockinfo where number not in ('sh000001','sz399001') ";
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				String code = rs.getString(1);
				
				il.deal(code);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
